import axios from './axios'
import router from "@/router";
import {useToast} from "vue-toastification"
import { ElLoading } from 'element-plus'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {getToken} from "@/core/util/app_util"

const toast = useToast()

export const post = (url:string, data?:any,showLoading=true,showError = true)=>{
    let loadingInstance:any = null
    if(showLoading){
        NProgress.configure({ showSpinner: false })
        NProgress.start()

        loadingInstance = ElLoading.service({
            lock: true,
            text: 'Loading',
            background: 'rgba(0, 0, 0, 0.7)',
        })
    }
    return new Promise((resolve, reject)=>{
        axios.post(url,data,{
            headers: {
                "Content-Type": 'application/json;charset=utf-8',
                token: getToken(),
            },
        }).then((res:any)=>{
            if(showLoading){
                loadingInstance.close()
                NProgress.done()
            }

            if(res!=null && res.response!=null && res.response.status!=200){
                if(showError) {
                    toast.error(res.message)
                    return reject(res)
                }
                return reject(res)
            }

            if (res.headers["content-disposition"] != null
                && res.headers["content-disposition"].indexOf("attachment") == 0) {
                //文件下载
                return resolve(res)
            }

            //检查数据是否返回NULL
            if (res.data === null) {
                return resolve(res)
            }

            if (res.data.code === 999) {
                if(showError) {
                    toast.error(res.data.msg)
                    return reject(res)
                }
            }

            //检查是否有权限
            if (res.data.code === 997) {
                router.push({
                    name: 'error-403',
                })
                return reject(res)
            }

            //检查登陆信息是否还存在
            if (res.data.code === 998) {
                router.push({
                    name: 'login',
                })
                return reject(res)
            }

            return resolve(res)
        }).catch((err:any)=>{
            if(showLoading){
                loadingInstance.close()
                NProgress.done()
            }

            return reject(err)
        })
    })
}

export const uploadFile = (url:string, formdata:any,showLoading=true,showError = true)=>{
    let loadingInstance:any = null
    if(showLoading){
        NProgress.configure({ showSpinner: false })
        NProgress.start()

        loadingInstance = ElLoading.service({
            lock: true,
            text: 'Loading',
            background: 'rgba(0, 0, 0, 0.7)',
        })
    }
    return new Promise((resolve, reject)=>{
        axios.post(url,formdata,{
            headers: {
                'Content-Type': 'multipart/form-data',
                token: getToken(),
            },
        }).then((res:any)=>{
            if(showLoading){
                loadingInstance.close()
                NProgress.done()
            }

            if(res!=null && res.response!=null && res.response.status!=200){
                if(showError) {
                    toast.error(res.message)
                    return reject(res)
                }
                return reject(res)
            }

            if (res.headers["content-disposition"] != null
                && res.headers["content-disposition"].indexOf("attachment") == 0) {
                //文件下载
                return resolve(res)
            }

            //检查数据是否返回NULL
            if (res.data === null) {
                return resolve(res)
            }

            if (res.data.code === 999) {
                if(showError) {
                    toast.error(res.data.msg)
                    return reject(res)
                }
            }

            //检查是否有权限
            if (res.data.code === 997) {
                router.push({
                    name: 'error-403',
                })
                return reject(res)
            }

            //检查登陆信息是否还存在
            if (res.data.code === 998) {
                router.push({
                    name: 'login',
                })
                return reject(res)
            }

            return resolve(res)
        }).catch((err:any)=>{
            if(showLoading){
                loadingInstance.close()
                NProgress.done()
            }

            return reject(err)
        })
    })
}